//
//  YHOAuthViewController.swift
//  YHWeibo
//
//  Created by 昱含 on 16/5/22.
//  Copyright © 2016年 YuHan. All rights reserved.
//

import UIKit
import SVProgressHUD

class YHOAuthViewController: UIViewController {
    
    let webView = UIWebView()
    

    override func viewDidLoad() {
        super.viewDidLoad()

       
    }
    
    override func loadView() {
        view = webView
        webView.delegate = self
        webView.opaque = false
        webView.backgroundColor = UIColor.whiteColor()
        setNavBar()
        loadOauthPage()
        
    }

    //TODO
    override func viewDidDisappear(animated: Bool) {
        super.viewDidDisappear(animated)
        SVProgressHUD.dismiss()
    }
    
    private func loadOauthPage(){
        let urlString = "https://api.weibo.com/oauth2/authorize?" + "client_id=" + client_id + "&redirect_uri=" + redirect_uri
        let url = NSURL(string: urlString)!
        let req = NSURLRequest(URL: url)
        webView.loadRequest(req)
        
        
    }

    
}

    extension YHOAuthViewController{
        
        private func setNavBar() {
            
            navigationItem.leftBarButtonItem = UIBarButtonItem(title: "关闭", target: self, action: #selector(YHOAuthViewController.close))
            
            navigationItem.rightBarButtonItem = UIBarButtonItem(title: "测试账户", target: self, action: #selector(YHOAuthViewController.testAccount))
        }
        
        @objc private func close() {
            
            dismissViewControllerAnimated(true, completion: nil)
        }
        
        @objc private func testAccount() {
            
            let jsString = "document.getElementById('userId').value = '18601327226', document.getElementById('passwd').value = 'chenyuhan830' "
//            18601327226
//            chenyuhan830
//            jaoyuan.student@sina.com
//            Hxy123Moon
            webView.stringByEvaluatingJavaScriptFromString(jsString)
            
        }
}

extension YHOAuthViewController: UIWebViewDelegate{
    
    func webViewDidStartLoad(webView: UIWebView) {
        SVProgressHUD.show()
    }
    
    func webViewDidFinishLoad(webView: UIWebView) {
        SVProgressHUD.dismiss()
    }
    
    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        
//        print(request.URL)
        
        let urlString = request.URL?.absoluteString ?? ""
//        print(urlString)
        
        let successStr = "code="
        if urlString.containsString(successStr) {
            
            let range = urlString.rangeOfString(successStr)!
            let code = urlString.substringFromIndex(range.endIndex)
//            print(code)
            
            YHUserAccountViewModel.sharedAccountViewModel.loadAccessToken(code, finished: { (isSuccess) in
                
                if !isSuccess {
                  
                    SVProgressHUD.showErrorWithStatus(appErrorTip)
                    return
                }
                
            //111
//                UIApplication.sharedApplication().keyWindow?.rootViewController = YHWelcomeViewController()
                NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootViewController, object: nil)
            })
            
            return false
            
        }
        
        return true
    }
}

